var baseUrl;

var isSaving = false;
function saveMenu(){
	var appId =$("#appId").val();
	
	var menuId = $("#s_menuId").val();
	var parentMenuId = $("#s_parentMenuId").val();
	
	var menuName = $.trim($("#s_menuName").val());
	
	var menuUrl = $.trim($("#s_menuUrl").val());
	var orderNum = $.trim($("#s_orderNum").val());

	var status = $.trim($("#s_menuStatus").val());
	
	if(!$("#s_menuName").valid() 
			|| !$("#s_menuUrl").valid() || !$("#s_orderNum").valid() ){
		return false;
	}

	var _data = {
			appId:appId,
			menuId:menuId,
			menuName:menuName,
			parentMenuId:parentMenuId,
			menuUrl:menuUrl,
			menuOrderNum:orderNum,
			menuStatus:status
	};
	if(isSaving){
		return;
	}
	isSaving = true;
	$.ajax({
		url:baseUrl+'/menu/save',
		type:"post",
		data:_data,
		dataType:"json",
		success:function(data){
			if(data.code=='0'){
				successTip();
				
				loadTree($("#appId").val());
				if(menuId == null || menuId == ''){
					$("#menuForm")[0].reset();
				}
				
				isSaving = false;
			}
		},
		error:function(){
			isSaving = false;
		}
	});
}
//禁用
function stopMenu(menuId){
	layer.confirm('停用菜单，如果其有下级菜单将会一起停用\n请确认是否停用菜单', {
  	    btn: ['确定', '取消']
  	    ,yes: function(index){
  	    	$.post(baseUrl+"/menu/stop/"+menuId,{},function(result){
  	  		if(result.code=="success"){
  	  			 successTip();
  	  			loadTree($("#appId").val());
  	  			//刷新
  	  			selectedMenu(menuId);
  	  		}else{
  	  			errorTipe();
  	  		}
  	  	},"json");
  	    }
  	});
}

//启用
function openMenu(menuId){
	layer.confirm('请确认是否启用菜单', {
  	    btn: ['确定', '取消']
  	    ,yes: function(index){
  	    	$.post(baseUrl+"/menu/open/"+menuId,{},function(result){
  	  		if(result.code=="success"){
  	  			 successTip();
  	  			loadTree($("#appId").val());
  	  			selectedMenu(menuId);
  	  		}else{
  	  			errorTip();
  	  		}
  	  	},"json");
  	    }
  	});
}

//删除
function deleteMenu(menuId){
	layer.confirm('删除菜单，如果其有下级菜单将会一起删除\n请确认是否删除菜单', {
  	    btn: ['确定', '取消']
  	    ,yes: function(index){
  	    	$.post(baseUrl+"/menu/delete/"+menuId,{},function(result){
  	  		if(result.code=="success"){
  	  			 successTip();
  	  			loadTree($("#appId").val());
  	  		    selectedMenu('0');
  	  		}else{
  	  			errorTip();
  	  		}
  	  	},"json");
  	    }
  	});
	
}

//增加同级
function addSameLevel(menuId,parentMenuId){
	if(menuId == null || menuId == ''){
		return;
	}
	$.ajax({
		url:baseUrl+'/menu/add/same/level/page/',
		type:"post",
		data:{menuId:menuId,parentMenuId:parentMenuId},
		dataType:"html",
		success:function(data){
			$("#menuInfo").html(data);
		},
		error:function(){
			
		}
	});
}

//增加下一级
function addNextLevel(menuId){
	if(menuId == null || menuId == ''){
		return;
	}
	
	$.ajax({
		url:baseUrl+'/menu/add/next/level/page',
		type:"post",
		data:{menuId:menuId},
		dataType:"html",
		success:function(data){
			$("#menuInfo").html(data);
		},
		error:function(){
			
		}
	});
}
//点击树菜单，显示该菜单信息
function selectedMenu(menuId){
//	alert('menuId='+menuId);
//	if(menuId == null || menuId == ''){
//		return;
//	}
	
	$.ajax({
		url:baseUrl+'/menu/get/menu/'+menuId,
		type:"post",
		data:{},
		dataType:"html",
		success:function(data){
			$("#menuInfo").html(data);
		},
		error:function(){
			
		}
	});
}   
function loadTree(appId){
	// alert('appId='+appId);
	//showTree([]);
	if($.trim(appId) == ''){
		$("#menuTree").html('');
		return;
	}
	$("#menuTree").html('<span class="text-red">加载中...</span>');
	if(appId != null && appId!=''){
		$.post(baseUrl+'/menu/query/menus/'+appId,{},function(data){
			if(data.code=='success'){
				
				var zNodes = new Array();
				var d = data.data;
				var z ;
				var o;
				var i;
				for(i=0;i<d.length;i++){
					z = d[i];
					o = {id:z.id,pId:z.parentMenuId,name:z.name,open:true,click:"selectedMenu('"+z.id+"');"};
					zNodes.push(o);
				}
				
				if(zNodes.length == 0){
					$("#menuTree").html('<span class="text-red">暂无菜单...</span>');
				}else{
					showTree(zNodes);
				}
				
			}else{
				//var zNodes = [];
				//showTree(zNodes);
				$("#menuTree").html('<span class="text-red">系统错误，获取应用对应的菜单失败！</span>');
				/*layer.msg('系统错误，获取应用对应的菜单失败', {
	  	            icon: 2
	  	        });*/
				errorTip();
			}
			//应用更改时，右侧的菜单信息清除
			selectedMenu('0');
		},"json");
	}else{
		var zNodes = [];
		showTree(zNodes);
	} 
}
 
          
 function showTree(zNodes){
  		var setting = {
              view: {
                  dblClickExpand: false,
                  showLine: true,
                  selectedMulti: false
              },
              data: {
                  simpleData: {
                      enable:true,
                      idKey: "id",
                      pIdKey: "pId",
                      rootPId: ""
                  }
              }
          };
          
          var t = $("#menuTree");
              $.fn.zTree.init(t, setting, zNodes);
              //新增部分代码，增加点击文字点击效果和编辑图标
             /* $("ul.ztree li:has('ul')>a").bind("click",function(){
                  if($(this).find("span.ico_open").length){
                      $(this).children("span:first-child").removeClass("ico_open").addClass("ico_close");
                      $(this).prev().removeClass("roots_open").addClass("roots_close");
                      if($(this).parent().find(".bottom_open").length){
                          $(this).prev().removeClass("bottom_open").addClass("bottom_close");
                      }
                      if($(this).parent().find(".center_open").length){
                          $(this).prev().removeClass("center_open").addClass("center_close");
                      }
                      $(this).siblings("ul").slideUp();
                  }else{
                      $(this).children("span:first-child").removeClass("ico_close").addClass("ico_open");
                      $(this).prev().removeClass("roots_close").addClass("roots_open");
                      if($(this).parent().find(".bottom_close").length){
                          $(this).prev().removeClass("bottom_close").addClass("bottom_open");
                      }
                      if($(this).parent().find(".center_close").length){
                          $(this).prev().removeClass("center_close").addClass("center_open");
                      }
                      
                      $(this).siblings("ul").slideDown();
                  }
              
              });
              $("ul.ztree li:not(:has('ul'))>a").bind("click",function(){
                  $("ul.ztree li:not(:has('ul')) a span:first-child").removeClass("edit");
                  $(this).children("span:first-child").addClass("edit");
               
              })*/
 }
          
       
  
  